package com.ymtms.settlement.controller;

import com.ymtms.settlement.pojo.CustomerGoodsbillBalance;
import com.ymtms.settlement.pojo.GoodsbillMaster;
import com.ymtms.settlement.service.CustomerGoodsbillBalanceService;
import com.ymtms.settlement.util.ValidateUtil;
import com.ymtms.settlement.vo.GridRequest;
import com.ymtms.settlement.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/goodsbill")
@Controller
public class CustomerGoodsbillBalanceController {

    @Autowired
    CustomerGoodsbillBalanceService customerGoodsbillBalanceService;

    /**
     * @description: 多条件分页查询可新增代收结算项
     * @param gridRequest 封装包含分页信息的类（当前页、页大小、排序字段、排序规则）
     * @return: GoodsbillMaster 包含查询条件实体类
     * @author: 张泓
     * @date: 2019/10/25
     */
    @RequestMapping("/gbmlist")
    @ResponseBody
    public GridResult<GoodsbillMaster> listGbm(
            GridRequest gridRequest,
            GoodsbillMaster goodsbillMaster,
            @RequestParam(value = "factDealDate", required = false) String factDealDate){
        String startTime = "";
        String endTime = "";
        if (ValidateUtil.isNotEmpty(factDealDate)) {
            String[] split = factDealDate.split(" - ");
            startTime = split[0];
            endTime = split[1];
            // System.out.println("startTime:"+startTime);
            // System.out.println("endTime:"+endTime);
        }
        // System.out.println("gridRequest:"+gridRequest);
        // System.out.println("goodsbillMaster:"+goodsbillMaster);
        // System.out.println("factDealDate:"+factDealDate);

        GridResult<GoodsbillMaster> result = customerGoodsbillBalanceService.queryGbmByPage(
                gridRequest, goodsbillMaster, startTime, endTime);
        result.setCode(0);
        result.setMsg("");
        System.out.println(result);
        return result;
    }

    @Transactional
    @RequestMapping("/add")
    @ResponseBody
    public Map add(CustomerGoodsbillBalance customerGoodsbillBalance,
                   @RequestParam("gmid") Integer gmid){
        Map result = new HashMap();
        // 1. 对前台传来的数据进行基本验证
        if(customerGoodsbillBalance == null){
            result.put("code",500);
            result.put("msg","数据传输错误");
            return result;
        }
        // 2. 执行添加和后台验证
        try {
            customerGoodsbillBalanceService.save(gmid, customerGoodsbillBalance);
            result.put("code",0);
            result.put("msg","结算成功");
        }catch(Exception e){
            result.put("code",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }

    /**
     * @description: 多条件分页查询代收结算历史
     * @param gridRequest 封装包含分页信息的类（当前页、页大小、排序字段、排序规则）
     * @param customerGoodsbillBalance 包含查询条件实体类
     * @param balanceDate 结算时间范围条件
     * @return: 返回类包含实体类集合和前端layui分页条件
     * @author: 张泓
     * @date: 2019/10/25
     */
    @RequestMapping("/cmblist")
    @ResponseBody
    public GridResult<CustomerGoodsbillBalance> listCmbHistory(
            GridRequest gridRequest, // 封装页面中laiyu表格所传过来的基本信息
            CustomerGoodsbillBalance customerGoodsbillBalance, // 实体类接收非范围参数
            @RequestParam(value = "balanceDate", required = false) String balanceDate){
//            @RequestParam Map<String, String> params
//            可以使用实体类来实现接收其他参数
        String startTime = "";
        String endTime = "";
        if (ValidateUtil.isNotEmpty(balanceDate)) {
            String[] split = balanceDate.split(" - ");
            startTime = split[0];
            endTime = split[1];

        }

        GridResult<CustomerGoodsbillBalance> result = customerGoodsbillBalanceService.queryCmbByPage(
                gridRequest, customerGoodsbillBalance, startTime, endTime);
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("limit")));
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(1,10);
        /*System.out.println(result.getData().get(1).getBalanceDate());*/
        result.setCode(0);
        result.setMsg("");
        System.out.println(result);
        return result;
    }

    /**
     * @description: 代收结算：软删除
     * @param cgsid 接收代收表主键
     * @return: 返回msg和code表示处理成功或失败
     * @author: 张泓
     * @date: 2019/10/24
     */
    @RequestMapping("/delete/{cgsid}")
    @ResponseBody
    public Map RemoveByCgsid(@PathVariable("cgsid")Integer cgsid){
        Map result = new HashMap();
        try {
            customerGoodsbillBalanceService.removeByCgsid(cgsid); // 删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    /**
     * @description: 代收结算：批量软删除
     * @param keys 接收代收表主键数组
     * @return: 返回msg和code表示处理成功或失败
     * @author: 张泓
     * @date: 2019/10/24
     */
    @PostMapping("/batchDelete/{keys}")
    @ResponseBody
    public Map batchRemoveByCgsid(@PathVariable("keys")Integer[] keys) {
        Map result = new HashMap();
        try {
            customerGoodsbillBalanceService.batchRemoveByCgsid(keys); // 批量删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

}
